package com.common.utils;

import org.apache.commons.lang3.ClassUtils;

import java.io.File;
import java.net.URISyntaxException;
import java.security.CodeSource;
import java.util.Objects;

/**
 * @author xin.ding
 * @date 2021/3/20 9:51
 */
public class PathUtils {

    /**
     * 获取程序当前运行路径
     *
     * @return
     */
    public static File getCurrentProgramPath() {
        CodeSource codeSource = ClassUtils.class.getProtectionDomain().getCodeSource();
        File jarFile = null;
        try {
            jarFile = new File(codeSource.getLocation().toURI().getPath());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        Objects.requireNonNull(jarFile, "jar file should not be null.");

        return jarFile.getParentFile();
    }
}
